package com.dk.controller;

import com.dk.dto.StockDTO;
import com.dk.em.Result;
import com.dk.page.PageResult;
import com.dk.pojo.Warehouse;
import com.dk.service.WarehouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("warehouse")
@Api(tags = "仓库控制器")
@RefreshScope       //开启热更新
public class WarehouseController {

    @Autowired
    private WarehouseService warehouseService;

    /*增加ok*/
    @PostMapping
    @ApiOperation("仓库增加接口")
    @ApiImplicitParam(name = "仓库对象信息",value = "Warehouse",required = false)
    public Result insertWarehouse(@RequestBody Warehouse warehouse){
        warehouseService.insertWarehouse(warehouse);
        return Result.OK();
    }


    /*根据id删除ok*/
    @DeleteMapping("deleteById")
    @ApiOperation("根据id删除")
    public Result deleteWarehouse(Integer id){
        warehouseService.deleteById(id);
        return Result.OK();
    }


    //批量删除ok
    @DeleteMapping
    @ApiOperation("根据id批量删除")
    public Result deleteAllWarehouse(@RequestBody List<String> ids){
        warehouseService.deleteAllById(ids);
        return Result.OK();
    }


    /*查询ok*/
    @GetMapping
    @ApiOperation("查询+分页")
    @ApiImplicitParams({@ApiImplicitParam(value = "pageNum", name = "当前页数", required = true),
                        @ApiImplicitParam(value = "pageSize", name = "每页条数", required = false)})
    public Result getAllwarehouse(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                  @RequestParam(value = "pageSize",defaultValue = "3")Integer pageSize){
        PageResult<Warehouse> pageInfo = warehouseService.getAllwarehouse(pageNum,pageSize);
        return Result.OK().setData(pageInfo);
    }

    //模糊查询ok
    @ApiOperation("根据仓库名称模糊查询")
    @ApiImplicitParam(name = "仓库名称",value = "name",required = true)//必填
    @GetMapping("getwarehouseByName")
    public Result getwarehouseByName(String name){
        List<Warehouse> warehouseList = warehouseService.getwarehouseByName(name);
        return Result.OK().setData(warehouseList);
    }


    //回显根据id查询数据ok
    @ApiImplicitParam(name = "仓库id",value = "id",required = true)
    @GetMapping("getById")
    public Result getById(Integer id){
        Warehouse warehouse = warehouseService.getById(id);
        return Result.OK().setData(warehouse);
    }


    /*修改*/
    @PutMapping
    @ApiOperation("修改仓库接口")
    @ApiImplicitParam(name = "仓库对象",value = "Warehouse",required = false)
    public Result updateWarehouse(@RequestBody Warehouse warehouse){
        warehouseService.updateWarehouse(warehouse);
        return Result.OK();
    }


    /*根据id查询仓库  给库存现有量提供接口*/
    @GetMapping("getwarById")
    public Result<Warehouse> getwarById(@RequestParam Integer warId){
       Warehouse warehouse =  warehouseService.getWarById(warId);
        return Result.OK().setData(warehouse);
    }
}
